package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.article.domain.Article;
import com.ruoyi.article.domain.Material;
import com.ruoyi.article.domain.TranslationRequest;
import com.ruoyi.article.domain.VoiceTranslation;
import com.ruoyi.article.dtos.PageResult;
import com.ruoyi.article.dtos.Result;
import com.ruoyi.article.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * asdController
 *
 * @author ruoyi
 * @date 2025-06-19
 */

@RestController
@RequestMapping("/article")
@Api("文章信息管理")
public class ArticleController extends BaseController {
    @Autowired
    private IArticleService articleService;

    @GetMapping("/listArticle")
    @ApiOperation("展示文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "5", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "文章标题关键词", dataType = "string", paramType = "query")
    })
    public Result<PageResult> listArticles(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            @RequestParam(required = false, defaultValue = "") String title) {
        PageInfo<Article> pageInfo = articleService.listArticles(pageNum, pageSize, title);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRecords(pageInfo.getList());
        return Result.success(pageResult);
    }

    @GetMapping("/getArticleInfo/{id}")
    @ApiOperation("获取单个文章信息")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "string", paramType = "path")
    public Result<Article> getInfo(@PathVariable("id") String id) {
        return Result.success(articleService.selectArticleById(id));
    }

    @PostMapping("/insrtArticle")
    @ApiOperation("添加文章")
    @ApiImplicitParam(name = "article", value = "文章对象", required = true, dataType = "Article", paramType = "body")
    public Result add(@RequestBody Article article) {
        return articleService.insertArticle(article);
    }

    @DeleteMapping("/removeArticle/{id}")
    @ApiOperation("删除文章")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "string", paramType = "path")
    public Result remove(@PathVariable String id) {
        return articleService.deleteArticleByIds(id);
    }

    @PutMapping("/updateArticleStatus")
    @ApiOperation("更改文章状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "isSublish", value = "发布状态(0:未发布 1:已发布)", required = true, dataType = "int", paramType = "query")
    })
    public Result updateArticleStatus(
            @RequestParam("id") Long id,
            @RequestParam("isSublish") int isSublish) {
        return articleService.updateArticleStatus(id, isSublish);
    }

    @PutMapping("/updateArticle")
    @ApiOperation("更新文章内容")
    @ApiImplicitParam(name = "article", value = "文章对象", required = true, dataType = "Article", paramType = "body")
    public Result updateArticle(@RequestBody Article article) {
        return articleService.updateArticle(article);
    }

    @PostMapping("/cnTranslationEn")
    @ApiOperation("文本翻译")
    @ApiImplicitParam(name = "translationRequest", value = "翻译请求参数", required = true, dataType = "TranslationRequest", paramType = "body")
    public Result cnTranslationEn(@RequestBody TranslationRequest translationRequest) {
        try {
            return articleService.cnTranslationEn(translationRequest);
        } catch (IOException e) {
            return Result.error("无法翻译");
        }
    }

    @PostMapping("/voiceTranslation")
    @ApiOperation("语音翻译")
    @ApiImplicitParam(name = "voiceTranslation", value = "语音翻译参数", required = true, dataType = "VoiceTranslation", paramType = "body")
    public Result voiceTranslation(@RequestBody VoiceTranslation voiceTranslation) {
        try {
            return articleService.voiceTranslation(voiceTranslation);
        } catch (IOException e) {
            return Result.error("无法语言翻译");
        }
    }

    @PostMapping("/upload_voice")
    @ApiOperation("语音上传")
    @ApiImplicitParam(name = "multipartFile", value = "语音文件", required = true, dataType = "__file", paramType = "form")
    public Result uploadVoice(MultipartFile multipartFile) {
        return articleService.uploadVoice(multipartFile);
    }
}